﻿#region License Information

/* •——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————•
   | Copyright Aptillon, Inc.                                                                                        |
   | This file is part of the CKS:API project - cksapi.codeplex.com                                                       |
   | Usage, modification and distribution subject to the license terms available here: http://cksapi.codeplex.com/license |
   |                                                                                                                      |
   | Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,             |
   | either express or implied.                                                                                           |
   |                                                                                                                      |
   | THIS MESSAGE MAY NOT BE LEGALLY REMOVED                                                                              |
   •——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————• */

#endregion


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CKS.API.Logging;
using System.Web.UI;
using CKS.API.Contracts;
using System.Web;

namespace CKS.API.Exceptions
{
    public class CKSExceptionDetails
    {
        public CKSException Exception { get; private set; }
        public LoggingSeverity Severity { get; set; }
        public CKSStatusBarColor StatusBarColor { get; set; }
        public ErrorDisplayType Display { get; set; }
        public string UserText { get; set; }
        public CKSLoggingCategory Category { get; private set; }
        public Control ThrowingControl { get; set; }

        public CKSExceptionDetails(CKSException ex, CKSLoggingCategory category)
        {
            Exception = ex;
            Category = category;
            SetDefaults();
        }

        public CKSExceptionDetails(Exception ex, CKSLoggingCategory category)
        {
            Exception = new CKSWrapperException(ex, category);
            Category = category;
            SetDefaults();
        }

        private void SetDefaults()
        {
            Severity = LoggingSeverity.Error;
            StatusBarColor = CKSStatusBarColor.blue;
            Display = ErrorDisplayType.StatusBar;
            UserText = Resources.FriendlyUserErrorText;
            ThrowingControl = GetThrower();

        }

        private Control GetThrower()
        {
            if (
                 (Check.ThatIsNotNull(HttpContext.Current))
                 && (Check.That(HttpContext.Current.Handler.ValidateIsType<Page>()))
                 )
            {
                return HttpContext.Current.CurrentHandler as Page;
            }
            return null;
        }
    }
}
